﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_PERMISSION.Base;

namespace ERP_RORZE_PRODUCT.Product.BasicList
{
    public partial class ctrlOption : DevExpress.XtraEditors.XtraUserControl
    {
        ClsBasicListDAO clsBasiclistDAO = new ClsBasicListDAO();
        ClsOptionTable b = new ClsOptionTable();
        DataTable dt;
        private LogUtils log;

        public ctrlOption()
        {
            InitializeComponent();
            log = new LogUtils();
        }
        void Reload(object sender, EventArgs e)
        {
            dt = clsBasiclistDAO.GetAllOption();
            gridControl1.DataSource = dt;
            gridView1.BestFitColumns();
        }
        private void btnReload_Click(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có thực sự muốn xóa dữ liệu!", "Message", MessageBoxButtons.OKCancel,
                                MessageBoxIcon.Question) == DialogResult.OK)
            {
                b.OptionID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "OptionID").ToString();
                clsBasiclistDAO.DeleteOption(b);

                //write Log
                log.Table = LogConstant.Table.Product.Options;
                log.Task = LogConstant.Task.Delete;
                log.Description = b.OptionID;
                log.WriteLog();

                Reload(sender, e);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frm_OptionAdd frmAdd = new frm_OptionAdd();
            frmAdd.Reload += new EventHandler(Reload);
            frmAdd.ShowDialog();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            frm_OptionEdit frmEdit = new frm_OptionEdit();
            frmEdit.OptionID = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "OptionID").ToString();
            frmEdit.OptionName = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "OptName").ToString();
            frmEdit.Labor = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Labor").ToString();
            frmEdit.Reload += new EventHandler(Reload);
            frmEdit.Show();
            Cursor.Current = Cursors.Default;

        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            Reload(sender, e);
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            btnAdd_Click(sender, e);
        }

        private void btEdit_Click(object sender, EventArgs e)
        {
            btnEdit_Click(sender, e);
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            btnDelete_Click(sender, e);
        }

        private void ctrlOption_Load(object sender, EventArgs e)
        {
            Reload(sender, e);
        }
    }
}
